﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FGHandler
{
    class lfsteam
    {
        private string _teamname;

        private List<lfsplayer> _teamlist = new List<lfsplayer>();

        public List<string> _teamUsernames;
        private int _totalMembers = 0;
        private int _disconnect = 0;

        private List<string[]> XMLMemberList = new List<string[]>();

        public lfsteam(string TeamName, int countMembers)
        {
            this._teamname = TeamName;
            this._totalMembers = countMembers;
            _teamUsernames = new List<string>(this._totalMembers);
            Console.WriteLine("New Team. " + TeamName + " with " + countMembers + " members");
        }

        public string TeamName
        {
            get { return this._teamname; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="username">A racer his LFS Username</param>
        /// <param name="realname">A racer his Real Name</param>
        /// <param name="nationality">A racer his nationality</param>
        public void AddMemberInfoFromXML(string username, string realname, string nationality)
        {
            if (!_teamUsernames.Contains(username))
                _teamUsernames.Add(username);
        }

        public void MemberXMLInfo(string[] XMLData)
        {
            XMLMemberList.Add(XMLData);
            _totalMembers++;
        }

        public void AddPlayer(lfsplayer player)
        {
            this._teamlist.Add(player);

            foreach (string[] xmldata in this.XMLMemberList)
            {
                if (xmldata[0] == player.Username)
                {
                    player.RealName = xmldata[1];
                    player.Nationality = xmldata[2];
                }
            }

            foreach (string s in _teamUsernames)
            {
                if (player.Username == s)
                {
                    Console.WriteLine("We can syncronize the info from XML file with LFS data. From the Team Racer");                    

                }
            }
        }

        /// <summary>
        /// Returns the total laps driving by every teammember in the team.
        /// </summary>
        public int GetTotalLaps
        {
            get {
            int totallaps = 0;            
            foreach (lfsplayer player in _teamlist)
                totallaps += player.LapList.Count;

            return totallaps;
            }
        }

        /// <summary>
        /// Calculate the average laptime from all the drivers including pitstop. Total laptimes / totallaps.
        /// </summary>
        public TimeSpan AvgLapTime
        {
            get
            {
                TimeSpan totaltime = new TimeSpan();
                int totallaps = 0;
                foreach (lfsplayer player in _teamlist)
                {
                    foreach (Lap lap in player.LapList)
                    {
                        totaltime = totaltime.Add(lap.TotalTime);
                        
                    }
                    totallaps += player.LapList.Count;
                    
                }
                return new TimeSpan(totaltime.Ticks / totallaps);
            }

        }

        /// <summary>
        /// Returns the currenlty active teammember on the track. Maybe not really needed.
        /// </summary>
        /// <returns></returns>
        public string getCurrentRacer()
        {
            foreach (lfsplayer player in _teamlist)
                if (player.TeamCurrent)
                    return player.Username;
            throw new Exception("Team.getCurrentRacer Can't find Current Racer");
        }


        public int Disconnects { get { return this._disconnect; } set { this._disconnect = value; } }



        public void Debug()
        {
            Console.WriteLine("Team " + TeamName + " Debug Information");
            Console.WriteLine(_teamlist.Count+  " members");
            foreach (lfsplayer player in this._teamlist)
            {                
                Console.WriteLine(player.ToString());                
            }
            
        }

       
    }
}
